package com.cart.dao;

import com.cart.entity.CartFinal;
import org.apache.ibatis.annotations.*;
import java.util.List;

@Mapper
public interface CartDao {
    //显示购物车
    @Select("select product.name,product.url,product.price,cart.num " +
            "from cart,product where cart.user_id = #{userId} and cart.product_id = product.id")
    List<CartFinal> selectCart(@Param("userId") int userId);

    //查看库存量
    @Select("select sum from repository where product_id = " +
            "(select id from product where name = #{fname})")
    List<Integer> selectSum(@Param("fname")String fname);

    //修改库存量
    @Update("update repository set sum = #{newSum} " +
            "where product.name = #{fname} and repository.product_id = product.id")
    void updateSum(@Param("newSum")int newSum,@Param("fname") String fname);

    //加入购物车
    @Insert("insert into cart values(user_id,product_id,num)" +
            "(#{userId},(select id from product where name = #{fname}),#{fnum})")
    void addToCart(@Param("userId")int userId,@Param("fname") String fname,@Param("fnum") int fnum);


}
